package com.leetcode.p100;

import com.bean.TreeNode;
import com.utils.TreeUtils;

public class Test112 {

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }

        if (root.val == targetSum && root.left == null && root.right == null) {
            return true;
        }

        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }

//    private boolean help(TreeNode root, int targetSum) {
//        if (root == null) {
//            return false;
//        }
//
//        if (targetSum == root.val && root.left == null && root.right == null) {
//            return true;
//        }
//
//        return help(root.left, targetSum - root.val) || help(root.right, targetSum - root.val);
//    }

    public static void main(String[] args) {
//        TreeNode root = TreeUtils.buildTree("5,4,8,11,null,13,4,7,2,null,null,null,1");
//        TreeNode root = TreeUtils.buildTree("5,4,null,11,null,null,2");
        TreeNode root = TreeUtils.buildTree("1,2");
        TreeUtils.showTree(root);

        Test112 test = new Test112();
        boolean result = test.hasPathSum(root, 1);
        System.out.println(result);
    }

}
